<?php
namespace Admin\Controller;
class EstateController extends CommonController{
    public function _initialize() {
		parent::_initialize();
        $this->_mod = D('Estate');
        $this->_cate_mod = D('AreaCate');
    }
    public function index (){
        $map = $this->_search();
        $mod = D($this->_name);
        !empty($mod) && $this->_list($mod, $map);
        $this->options = $this->check();
        $big_menu = array(
            'title' => '添加小区',
            'iframe' => U('Estate/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '360'
        );
        $this->assign('big_menu', $big_menu);
        $this->display();
    }
    public function _before_index() {
        $p = I('p',1,'intval');
        /*$this->_data = array(
            'usedAdd'    => array(
                'c' => 'Used',
                'a' => 'add',
                'str'    => '<a href="%s">添加二手房源</a>',
                'param' => array('eid'=>'@id@','menuid'=>259),
                'isajax' => 0,
                'replace'=> ''
            ),
            'RentalAdd'    => array(
                'c' => 'Rental',
                'a' => 'add',
                'str'    => '<a href="%s">添加出租房源</a>',
                'param' => array('eid'=>'@id@','menuid'=>251),
                'isajax' => 0,
                'replace'=> ''
            )
        );*/
        $this->assign('p',$p);
        $this->list_relation = true;
    }

    protected function _search() {
        $map = array();
        ($time_start = I('get.time_start')) && $map['add_time'][] = array('egt', strtotime($time_start));
        ($time_end = I('get.time_end')) && $map['add_time'][] = array('elt', strtotime($time_end)+(24*60*60-1));
        ($keyword = I('get.keyword')) && $map['title'] = array('like', '%'.$keyword.'%');
        $area_id = I('get.area_id',0,'int');
		if( $_GET['status']==null ){
            $status = -1;
        }else{
            $status = intval($_GET['status']);
        }
        $status>=0 && $map['status'] = array('eq',$status);
        $selected_ids = '';
        if ($area_id) {
            $id_arr = $this->_cate_mod->get_child_ids($area_id, true);
            $map['area_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id'=>$area_id))->getField('spid');
            $selected_ids = $spid ? $spid . $area_id : $area_id;
        }
        $this->assign('search', array(
            'time_start' => $time_start,
            'time_end' => $time_end,
            'area_id' => $area_id,
            'selected_ids' => $selected_ids,
            'status'  => $status,
            'keyword' => $keyword
        ));
        return $map;
    }

    protected function _before_insert($data) {
        $data['data']       = serialize($data['data']);
        $data['house_type'] = !empty($data['house_type']) ? '|'.implode('|',$data['house_type']).'|' : '';
        $data['add_time']   = time();
        return $this->_insertPic($data);
    }

    public function _before_edit(){
        $id = I('get.id');
        $info = $this->_mod->field('id,area_id,data,file,house_type')->where(array('id'=>$id))->find();
        $spid = $this->_cate_mod->where(array('id'=>$info['area_id']))->getField('spid');
        if( $spid==0 ){
            $spid = $info['area_id'];
        }else{
            $spid .= $info['area_id'];
        }
        $this->assign('img_list',json_decode($info['file'],true));
        $this->assign('data',unserialize($info['data']));
        $this->assign('selected_ids',$spid);
        $this->assign('house_type',explode('|',trim($info['house_type'],'|')));
    }

    protected function _before_update($data) {
        $data['house_type'] = !empty($data['house_type']) ? '|'.implode('|',$data['house_type']).'|' : '';
        $data['data'] = serialize($data['data']);
        $new_data = $this->_updatePic($data);
        $info = M('estate')->where(array('id'=>$new_data['id']))->find();
        if($info['is_index'] == 1 && $new_data['img'] && $_FILES['img']['name']){
            $info['img'] = $new_data['img'];
            D('Position')-> where(array('hid'=>$info['id'],'model'=>'estate'))->save(array('data'=>serialize($info)));
        }
        return $data;
    }

    public function ajaxCheckTitle(){
        $title = urldecode(I('get.J_title',''));
        if($title){
            $info = $this->_mod->where(array('title'=>$title))->find();
            if($info){
                $this->ajaxReturn(0);
            }else{
                $this->ajaxReturn(1);
            }
        }else{
            $this->ajaxReturn(1);
        }
    }
    public function ajax_getchilds() {
        $id = I('id',0,'intval');
        if ($id <= 0)$return = $this->_mod->field('id,title as name')->select();
        if ($return) {
            $this->ajaxReturn(1, L('operation_success'), $return);
        } else {
            $this->ajaxReturn(0, L('operation_failure'));
        }
    }

}